package com.baseDemo.web.service.systeminfo.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.baseDemo.web.dao.systeminfo.SystemInfoEntityDao;
import com.baseDemo.web.entity.SystemInfoEntity;
import com.baseDemo.web.server.SystemServer;
import com.baseDemo.web.service.systeminfo.SystemInfoService;
import com.share.hibernate.baseDaoSupport.PageUtil;
import com.share.service.baseServiceSupport.impl.BaseServiceSupport;
@Service
public class SystemInfoServiceImpl extends BaseServiceSupport<SystemInfoEntity> implements SystemInfoService{
	@Resource private SystemInfoEntityDao systemInfoEntityDao;
	@Resource private PageUtil pageUtil;
	private Logger logger = Logger.getLogger(getClass());
	@Override
	public void init() {
		logger.info("初始化系统属性");
		List<SystemInfoEntity> list = systemInfoEntityDao.find();
		SystemServer systemServer = SystemServer.getInstance();
		if(list!=null&&list.size()>0){
			for (SystemInfoEntity systemInfoEntity : list) {
				systemServer.set(systemInfoEntity.getKey(), systemInfoEntity.getValue());
			}
		}
		logger.info("初始化系统属性完成");
	}
	@Override
	public PageUtil getListForPageToJson(Integer start, Integer limit) {
		Integer rowTotal = systemInfoEntityDao.getPageTotal();
		pageUtil.setTotal(rowTotal);
		List<SystemInfoEntity> list = systemInfoEntityDao.find(limit, start);
		pageUtil.setEntityList(list);
		return pageUtil;
	}
	@Override
	public String addNewSystemInfo(SystemInfoEntity systemInfoEntity) {
		String id = systemInfoEntityDao.save(systemInfoEntity);
		if(!"0".equals(id)){
			SystemServer.getInstance().set(systemInfoEntity.getKey(), systemInfoEntity.getValue());
		}
		return "{'id':'" + id +"'}";
	}
	@Override
	public String updateSystemInfo(SystemInfoEntity systemInfoEntity) {
		String status = systemInfoEntityDao.update(systemInfoEntity);
		if(!"0".equals(status)){
			SystemServer.getInstance().set(systemInfoEntity.getKey(), systemInfoEntity.getValue());
		}
		return status;
	}
}
